import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:god_logistics/utils/eventbus.dart';
import 'package:god_logistics/utils/http.dart';
import 'package:god_logistics/utils/showToast.dart';
import 'package:god_logistics/utils/tokenManage.dart';

class Minepage extends StatefulWidget {
  Minepage({super.key, required this.currentIndex});

  int currentIndex;

  @override
  State<Minepage> createState() => _MinepageState();
}

class _MinepageState extends State<Minepage> {
  var userinfo = {};

  @override
  void initState() {
    super.initState();
    eventBus.on<RefreshMine>().listen((event) {
      getProfile();
    });

    eventBus.on<BackMine>().listen((event) {
      getProfile();
    });
  }

  void getProfile() async {
    var res = await http.get('/driver/users');
    if (res['code'] != 200) return ToastUtil.showError('获取数据失败');
    setState(() {
      userinfo = res['data'];
    });
  }

  @override
  void didUpdateWidget(covariant Minepage oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.currentIndex == 2) {
      getProfile();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      color: const Color.fromARGB(255, 245, 245, 245),
      child: Stack(
        children: [
          Positioned(
            top: 0,
            left: 0,
            child: Container(
              width: MediaQuery.of(context).size.width,
              height: 300,
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/icons/bg_page_my.jpg'),
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ),
          // ---------------------------- 顶部 司机基本信息 -------------------
          Positioned(
            top: 50,
            left: 60,
            child: Container(
              width: 250,
              height: 210,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  // ----------------- 头像 -----------------
                  SizedBox(height: 20),
                  Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(100),
                      color: Colors.white,
                    ),
                    child: SvgPicture.asset(
                      'assets/icons/ic_avatar_driver.svg',
                      // colorFilter: ColorFilter.mode(
                      //   Colors.red, // 想要的颜色
                      //   BlendMode.srcIn, // 混合模式，srcIn通常用于替换颜色
                      // ),
                    ),
                  ),
                  Container(
                    child: Text(
                      '${userinfo['name']}',
                      style: TextStyle(fontSize: 18, color: Colors.white),
                    ),
                  ),
                  Container(
                    child: Column(
                      children: [
                        Text(
                          '司机编号：' + '${userinfo['number']}',
                          style: TextStyle(color: Colors.white),
                        ),
                        Text(
                          '手机号： ' + '${userinfo['phone']}',
                          style: TextStyle(color: Colors.white),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),

          // ------------------------- 中间部分 本月任务 -------------------------
          Positioned(
            top: 270,
            left: 16,
            child: Container(
              width: MediaQuery.of(context).size.width * 0.9,
              height: 110,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 10, bottom: 10),
                    child: Text(
                      '- 本月任务 -',
                      style: TextStyle(fontSize: 16, color: Colors.grey),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(10),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Container(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [Text('18'), Text('任务总量')],
                            ),
                          ),
                          Container(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [Text('16'), Text('完成任务量')],
                            ),
                          ),
                          Container(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [Text('3987'), Text('运输里程(km)')],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),

          //  -------------------------- 底部 三个设置跳转按钮 ----------------
          Positioned(
            left: 16,
            bottom: 5,
            child: Container(
              width: MediaQuery.of(context).size.width * 0.9,
              height: 130,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
              ),
              child: Column(
                children: [
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(color: Colors.grey)),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text('车辆信息'),
                          SvgPicture.asset('assets/icons/ic_btn_more.svg'),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(color: Colors.grey)),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text('任务数据'),
                          SvgPicture.asset('assets/icons/ic_btn_more.svg'),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        // TODO
                        Navigator.pushNamed(context, '/settingpage');
                      },
                      child: Container(
                        padding: EdgeInsets.all(10),
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(color: Colors.grey),
                          ),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('系统设置'),
                            SvgPicture.asset('assets/icons/ic_btn_more.svg'),
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
